import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:最小的K个数
 * User: wangxin
 * Date: 2025-03-09
 * Time: 21:23
 */
public class Test {
    public ArrayList<Integer> GetLeastNumbers_Solution (int[] input, int k) {
        // write code here、
        ArrayList<Integer> res = new ArrayList<>();
        if(input == null || input.length == 0||k == 0){
            return res;
        }
        PriorityQueue<Integer> q = new PriorityQueue<>((o1,o2)->o2.compareTo(o1));
        for(int i = 0;i <k;i++){
            q.offer(input[i]);
        }
        for(int i = k;i <input.length;i++){
            if(q.peek() > input[i]){
                q.poll();
                q.offer(input[i]);
            }
        }
        for(int i = 0;i < k;i++){
            res.add(q.poll());
        }
        return res;
    }
}
